package com.itheima.ball;

import java.util.Arrays;
import java.util.Random;
/*
生成双色球
 */
public class ShuangSeQiu {
    private static Random r = new Random();

    private ShuangSeQiu() {

    }

    public static int[] generate() {
        int[] arr = new int[6];
        for (int i = 0; i < arr.length; ) {
            int hong = getHong();
            int index = getIndex(arr, hong);

            if (index < 0) {
                int before1 = getIndex(arr, hong - 1);
                int before2 = getIndex(arr, hong - 2);
                int before3 = getIndex(arr, hong - 3);
                int after1 = getIndex(arr, hong + 1);
                int after2 = getIndex(arr, hong + 2);
                int after3 = getIndex(arr, hong + 3);
                if (before1 >= 0 && before2 >= 0 && before3 >= 0) {
                    continue;
                }
                if (before1 >= 0 && before2 >= 0 && after1 >= 0) {
                    continue;
                }
                if (before1 >= 0 && after1 >= 0 && after2 >= 0) {
                    continue;
                }
                if (after1 >= 0 && after2 >= 0 && after3 >= 0) {
                    continue;
                }
                arr[i] = hong;
                i++;
            }
        }
        Arrays.sort(arr);
        int[] result = new int[7];
        System.arraycopy(arr, 0, result, 0, arr.length);
        result[6] = getLan();
        return result;

    }

    private static int getHong() {
        return r.nextInt(33) + 1;
    }

    private static int getLan() {
        return r.nextInt(16) + 1;
    }

    private static int getIndex(int[] arr, int number) {
        if (arr == null || arr.length < 0) {
            return -1;
        }
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == number) {
                return i;
            }
        }
        return -1;
    }

}




